package com.zmn.plat.services.impl.blacklist;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.blacklist.Blacklist;
import com.zmn.plat.model.entity.blacklist.query.BlacklistQuery;
import com.zmn.plat.persistence.interfaces.blacklist.BlacklistDao;
import com.zmn.plat.services.interfaces.blacklist.BlacklistService;

/**
 * 描述: 黑名单接口类实现
 *
 * @author wangjie
 * @since 2019-07-31
 **/
@Service
public class BlacklistServiceImpl implements BlacklistService {

    private BlacklistDao blacklistDao;

    @Autowired
    public BlacklistServiceImpl(BlacklistDao blacklistDao) {
        this.blacklistDao = blacklistDao;
    }

    /**
     * 描述: 根据黑名单id 查询数据
     *
     * @param blackId
     *            黑名单id
     * @param blackType
     *            黑名单类型
     * @return com.zmn.plat.model.entity.blacklist.Blacklist 黑名单
     **/
    @Override
    public Blacklist findByIdAndType(Integer blackId, Integer blackType) {

        if (Objects.isNull(blackId)) {
            throw new NullPointerException("黑名单id不能为null");
        }
        if (Objects.isNull(blackType)) {
            throw new NullPointerException("黑名单类型不能为null");
        }
        return blacklistDao.findByIdAndType(blackId, blackType);
    }

    /**
     * 描述: 根据黑名单类容，类型，查询黑名单数据
     *
     * @param blackNum
     *            黑名单内容
     * @param blackType
     *            黑名单类型
     * @return com.zmn.plat.model.entity.blacklist.Blacklist
     **/
    @Override
    public Blacklist findByBlackNumAndType(String blackNum, Integer blackType) {

        if (Objects.isNull(blackNum)) {
            throw new NullPointerException("黑名单内容不能为null");
        }
        if (Objects.isNull(blackType)) {
            throw new NullPointerException("黑名单类型不能为null");
        }
        BlacklistQuery query = new BlacklistQuery();
        query.setBlacklistNum(blackNum);
        query.setBlacklistType(blackType);
        return Optional.ofNullable(blacklistDao.listByQuery(query)).orElse(Collections.emptyList()).stream().filter(ff -> Objects.equals(ff.getBlacklistNum(), blackNum)).findFirst()
            .orElse(new Blacklist());
    }

    /**
     * 描述: 根据条件查询黑名单数据 分页
     *
     * @param query
     *            查询条件
     * @return java.util.List<com.zmn.plat.model.entity.blacklist.Blacklist>
     **/
    @Override
    public List<Blacklist> listPageByQuery(BlacklistQuery query) {

        Integer count = blacklistDao.countByQuery(query);
        query.setTotalCount(count);
        return Objects.equals(count, GlobalConsts.NONE) ? Collections.emptyList() : blacklistDao.listPageByQuery(query);
    }

    /**
     * 描述: 根据条件查询黑名单数据 不分页
     *
     * @param query
     *            查询条件
     * @return java.util.List<com.zmn.plat.model.entity.blacklist.Blacklist>
     **/
    @Override
    public List<Blacklist> listByQuery(BlacklistQuery query) {
        return blacklistDao.listByQuery(query);
    }

    /**
     * 描述: 根据白名单内容查询数据
     *
     * @param blackNum
     *            白名单内容
     * @return java.util.List<com.zmn.plat.model.entity.whitelist.Whitelist>
     * @author wangjie
     * @since 2019/8/2
     **/
    @Override
    public List<Blacklist> listByBlackNum(List<String> blackNum) {

        if (CollectionUtils.isEmpty(blackNum)) {
            throw new NullPointerException("白名单内容不能为null或者空集合");
        }
        return blacklistDao.listByBlackNum(blackNum);
    }

    /**
     * 描述: 新增黑名单
     *
     * @param blacklist
     *            黑名单
     **/
    @Override
    public void insertBlack(Blacklist blacklist) {

        if (Objects.isNull(blacklist)) {
            throw new NullPointerException("新增时黑名单不能为null");
        }
        blacklist.setCreateTime(DateUtil.getNow());
        blacklistDao.insertBlack(blacklist);
    }

    /**
     * 描述: 删除黑名单数据
     *
     * @param blackId
     *            黑名单id
     * @param blackType
     *            黑名单类型
     **/
    @Override
    public void deleteBlackByIdAndType(Integer blackId, Integer blackType) {

        if (Objects.isNull(blackId)) {
            throw new NullPointerException("黑名单id不能为null");
        }
        if (Objects.isNull(blackType)) {
            throw new NullPointerException("黑名单类型不能为null");
        }
        blacklistDao.deleteBlackByIdAndType(blackId, blackType);
    }
}
